package com.example.controller;

import com.example.utils.Result;
import com.example.entity.Eaasy;
import com.example.service.EaasyService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/eaasy")
public class EaasyController {

    @Resource
    EaasyService eaasyService;

    // 添加论文接口
    @PostMapping("/add")
    @CrossOrigin(origins = "*")
    public Result add(@RequestBody Eaasy eaasy, HttpSession session) {
        // 设置论文作者ID为当前会话中的用户ID
        eaasy.setId((Integer) session.getAttribute("id"));
        String msg = eaasyService.add(eaasy);
        if (msg != null) {
            return Result.error(msg);
        }
        return Result.success();
    }

    // 删除论文接口
    @DeleteMapping("/delete/{id}")
    @CrossOrigin(origins = "*")
    public Result delete(@PathVariable Integer id) {
        eaasyService.deleteById(id);
        return Result.success();
    }

    // 获取论文详情接口
    @GetMapping("/detail/{id}")
    @CrossOrigin(origins = "*")
    public Result detail(@PathVariable Integer id) {
        return Result.success(eaasyService.getById(id));
    }

    // 更新论文接口
    @PutMapping("/update")
    @CrossOrigin(origins = "*")
    public Result update(@RequestBody Eaasy eaasy) {
        eaasyService.updateById(eaasy);
        return Result.success();
    }

    // 分页查询论文接口
    @GetMapping("/selectPage")
    @CrossOrigin(origins = "*")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Eaasy eaasy,
                             HttpSession session) {
        // 设置查询条件中的作者ID为当前会话中的用户ID
        eaasy.setId((Integer) session.getAttribute("id"));
        PageInfo<Eaasy> pageInfo = eaasyService.selectPage(pageNum, pageSize, eaasy);
        return Result.success(pageInfo);
    }
}
